<?php
/**
 * Created by PhpStorm.
 * User: huyang
 * Date: 16/10/29
 * Time: 下午8:02
 */

namespace App\Http\Repositories\Services\Impls;


use App\Exceptions\ServiceException;
use App\Http\Constants\CommonExceptionConstants;
use App\Http\Repositories\Daos\IArticleDao;
use App\Http\Repositories\Daos\ITypeDao;
use App\Http\Repositories\Eloquent\Article;
use App\Http\Repositories\Services\IArticle;
use App\Http\Requests\Admin\ArticleInputDTO;
use App\Util\Helper;
use Illuminate\Support\Facades\Config;

class ArticleService implements IArticle
{

    private $articleDao;

    private $typeDao;

    public function __construct(IArticleDao $articleDao, ITypeDao $typeDao)
    {
        $this->articleDao = $articleDao;
        $this->typeDao = $typeDao;
    }

    public function byId($id)
    {
        $data = $this->articleDao->find($id);
        if (!$data) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        if (empty($data->cover)) {
            $data->cover = Config::get('constant.default_image');
        }
        $this->articleDao->increment('click',1,['id'=>$id]);
        return $data;
    }

    public function store(ArticleInputDTO $inputDTO)
    {
        $dao = new Article();
        $dao->type_id = $inputDTO->post('type_id');
        $dao->title = $inputDTO->post('title');
        $dao->abstract = $inputDTO->post('abstract');
        $dao->cover = $inputDTO->post('cover');
        $dao->jump = $inputDTO->post('jump');
        $dao->video = $inputDTO->post('video');
        $dao->content = $inputDTO->post('content');
        $dao->tag = $inputDTO->post('tag');
        $dao->author = $inputDTO->post('author');
        $dao->source = $inputDTO->post('source');
        $dao->click = $inputDTO->post('click');
        $dao->coin = $inputDTO->post('coin');
        $dao->date = $inputDTO->post('date');

        $dao->save();
    }

    public function update(ArticleInputDTO $inputDTO, $id)
    {
        $dao = $this->articleDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        $dao->type_id = $inputDTO->post('type_id');
        $dao->title = $inputDTO->post('title');
        $dao->abstract = $inputDTO->post('abstract');
        $dao->cover = $inputDTO->post('cover');
        $dao->jump = $inputDTO->post('jump');
        $dao->video = $inputDTO->post('video');
        $dao->content = $inputDTO->post('content');
        $dao->tag = $inputDTO->post('tag');
        $dao->author = $inputDTO->post('author');
        $dao->source = $inputDTO->post('source');
        $dao->click = $inputDTO->post('click');
        $dao->coin = $inputDTO->post('coin');
        $dao->date = $inputDTO->post('date');

        $dao->update();
    }

    public function lists($search, $page = 0, $size = 10)
    {
        $data = $this->articleDao->lists($search, $page, $size);
        if (isset($data['results']) && $data['results']) {
            $ids = Helper::returnArrayVal($data['results'],'type_id');
            $map = $this->typeDao->byIds($ids);
            $map = Helper::arrayMapByKey($map->toArray(),'id','name');
            foreach ($data['results'] as &$item) {
                $item['date'] = substr($item['date'], 0, 10);
                if (isset($item['cover']) && empty($item['cover'])) {
                    $item['cover'] = Config::get('constant.default_image.src');
                }
                if (!empty($item['type_id']) && !empty($map[$item['type_id']])) {
                    $item['typeName'] = $map[$item['type_id']];
                } else {
                    $item['typeName'] = '';
                }
            }
        }
        return $data;
    }

    public function state($id, $state)
    {
        $where = ['id'=>['symbol'=>'in','val'=>explode(',',$id)]];
        $this->articleDao->update($where, ['state'=>$state]);
    }

    public function agreement($search)
    {
        $data = $this->articleDao->agreement($search);
        if (!$data) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        return $data;
    }

}